<style>
    th, td {
        text-align: center;
        vertical-align: middle !important;
    }
</style>

{php}
// 解析JSON数据
$densityArray = !empty($row['density']) ? json_decode(trim($row['density'], '"'), true) : [];
$densityCount = count($densityArray);
$thicknessArray = !empty($row['thickness']) ? json_decode(trim($row['thickness'], '"'), true) : [];
$thicknessCount = count($thicknessArray);
{/php}

<div class="table-responsive">
    <table class="table table-bordered" style="text-align: center;" id="printable-table">
        <thead>
            <tr>
                <th>项目</th>
                <th>单位</th>
                <th>测试方法</th>
                <th>测试结果</th>
                <th>公差</th>
            </tr>
        </thead>
        <tbody>
            <!-- 密度部分 - 动态生成行 -->
            {if condition="!empty($densityArray)"}
                {foreach $densityArray as $key => $density}
                <tr>
                    {if condition="$key == 0"}
                    <td rowspan="{$densityCount}">密度<br>Density</td>
                    <td rowspan="{$densityCount}">g/cm²</td>
                    <td rowspan="{$densityCount}">JISK6401</td>
                    {/if}
                    <td>{$density}</td>
                    {if condition="$key == 0"}
                    <td rowspan="{$densityCount}">±{$row.densityallowance}%</td>
                    {/if}
                </tr>
                {/foreach}
            {else /}
                <tr>
                    <td rowspan="1">密度<br>Density</td>
                    <td rowspan="1">g/cm²</td>
                    <td rowspan="1">JISK6401</td>
                    <td>-</td>
                    <td rowspan="1">±{$row.densityallowance}%</td>
                </tr>
            {/if}
            
            <!-- 厚度部分 - 修改为与密度相同的结构 -->
            {if condition="!empty($thicknessArray)"}
            {foreach $thicknessArray as $key => $thickness}
            <tr>
                {if condition="$key == 0"}
                <td rowspan="{$thicknessCount}">厚度<br>Thickness</td>
                <td rowspan="{$thicknessCount}">mm</td>
                <td rowspan="{$thicknessCount}">Thickness gauge</td>
                {/if}
                <td>{$thickness}</td>
                {if condition="$key == 0"}
                <td rowspan="{$thicknessCount}">±{$row.thicknessallowance}%</td>
                {/if}
            </tr>
            {/foreach}
            {else /}
            <tr>
                <td rowspan="1">厚度<br>Thickness</td>
                <td rowspan="1">mm</td>
                <td rowspan="1">Thickness gauge</td>
                <td>-</td>
                <td rowspan="1">±{$row.thicknessallowance}%</td>
            </tr>
            {/if}

            <!-- 其他行保持不变 -->
            <tr>
                <td rowspan="2">25%壓縮荷重<br>25%CLD</td>
                <td>Mpa</td>
                <td rowspan="2">ASTMD3574</td>
                <td>{$row.cld}</td>
                <td>±{$row.cldallowance}</td>
            </tr>
            <tr>
                <td>kg/cm²</td>
                <td>{$row.cld*10}</td>
                <td>±{$row.cldallowance*10}</td>
            </tr>

            <tr>
                <td rowspan="2">壓缩永久形</td>
                <td rowspan="2">%</td>
                <td>ASTM D 3574-95 Test D @23℃</td>
                <td>{$row.astm23}</td>
                <td>≤5%</td>
            </tr>
            <tr>
                <td>ASTM D 3574-95 Test D @70℃</td>
                <td>{$row.astm70}</td>
                <td>≤10%</td>
            </tr>
           
            <tr>
                <td>建议长期使用温度</td>
                <td>℃</td>
                <td></td>
                <td>80</td>
                <td></td>
            </tr>
            <tr>
                <td>建议短期使用温度</td>
                <td>℃</td>
                <td></td>
                <td>120</td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>

<script>
    function printTable() {
        var tableContent = document.getElementById("printable-table").outerHTML;
        var printWindow = window.open('', '', 'width=800,height=600');
    
        printWindow.document.write(`
            <html>
                <head>
                    <title>打印表格</title>
                    <style>
                        table {
                            width: 100%;
                            border-collapse: collapse;
                        }
                        table, th, td {
                            border: 1px solid black;
                        }
                        th, td {
                            padding: 8px;
                            text-align: center;
                            vertical-align: middle !important;
                        }
                    </style>
                </head>
                <body>
                    ${tableContent}
                </body>
            </html>
        `);
    
        printWindow.document.close();
        printWindow.print();
    }
</script>